package com.ellecity06.common.ktx

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.annotation.Size
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.ellecity06.common.utils.ToastUtils
import com.ellecity06.common.widgets.dialog.showAlert
import com.luck.picture.lib.permissions.PermissionUtil.hasPermissions
import com.permissionx.guolindev.PermissionX

/**
 * @author ellecity06
 * @time 2024/11/4 15:48
 * @des
 */
fun FragmentActivity.requestPermission(vararg permissions: String) {

}

fun FragmentActivity.requestPhoneStatePermission() {
    PermissionX.init(this).permissions(Manifest.permission.READ_PHONE_STATE).request { allGranted, grantedList, deniedList ->
    }
}

/**
 * 请求录音权限
 */
fun FragmentActivity.requestAudioPermission(block: () -> Unit) {
    if (!hasAudioPermission()) {
        showAlert(content = "录音需要您的录音权限") {
            PermissionX.init(this).permissions(Manifest.permission.RECORD_AUDIO)
                .request { allGranted, grantedList, deniedList ->
                    if (allGranted) {
                        block.invoke()
                    } else {
                        ToastUtils.show("您拒绝了权限")
                    }

                }
        }
    } else {
        block.invoke()
    }


}

/**
 * desc 权限提示的文案
 */
fun FragmentActivity.requestStorePermission(
    desc: String,
    block: (allGranted: Boolean, grantedList: List<String>, deniedList: List<String>) -> Unit
) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        block.invoke(true, mutableListOf(), mutableListOf())
    } else {
        val list = listOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        if (!hasPermissions(list)) {
            showAlert(content = desc, confirmText = "申请权限") {
                PermissionX.init(this).permissions(list)
                    .request { allGranted, grantedList, deniedList ->
                        if (allGranted) {
                            block.invoke(allGranted, grantedList, deniedList)
                        } else {
                            ToastUtils.show("您拒绝了权限")
                        }

                    }
            }
        } else {
            block.invoke(true, mutableListOf(), mutableListOf())
        }
    }


}

/**
 * 是否拥有文件读写权限，没有的话需要先弹窗描述权限说明
 */
fun Activity.hasReadImagePermission(content: String = "更换头像选取图片需要开启您的存储权限", block: () -> Unit) {
    if (!hasReadImagePermission()) {
        showAlert(content = content) {
            block.invoke()
        }
    } else {
        block.invoke()
    }

}

/**
 * 是否拥有文件读写权限，没有的话需要先弹窗描述权限说明
 */
fun Activity.hasReadVideoPermission(content: String = "选择视频需要您授予读取相册视频的权限", block: () -> Unit) {
    if (!hasReadVideoPermission()) {
        showAlert(content = content) {
            block.invoke()
        }
    } else {
        block.invoke()
    }

}

/**
 * 是否拥有文件读写权限，没有的话需要先弹窗描述权限说明
 */
fun Activity.hasReadAudioPermission(content: String = "选择音频需要您授予读取相册音频的权限", block: () -> Unit) {
    if (!hasReadVideoPermission()) {
        showAlert(content = content) {
            block.invoke()
        }
    } else {
        block.invoke()
    }

}

fun FragmentActivity.requestCameraPermission(block: () -> Unit) {
    if (!hasPermissions(mutableListOf(Manifest.permission.CAMERA))) {
        showAlert(content = "更换头像拍照需要开启您的相机权限") {
            PermissionX.init(this).permissions(Manifest.permission.CAMERA).request { allGranted, grantedList, deniedList ->
                block.invoke()
            }
        }
    } else {
        block.invoke()
    }
}

/**
 * 是否拥有相机权限，没有的话需要先弹窗描述权限说明
 */
fun Activity.hasCamera(block: () -> Unit) {
    if (!hasPermissions(mutableListOf(Manifest.permission.CAMERA))) {
        showAlert(content = "更换头像拍照需要开启您的相机权限") {
            block.invoke()
        }
    } else {
        block.invoke()
    }

}

/**
 * 是否拥有录音权限，没有的话需要先弹窗描述权限说明
 */
fun Activity.hasAudio(block: () -> Unit) {
    if (!hasPermissions(mutableListOf(Manifest.permission.RECORD_AUDIO))) {
        showAlert(content = "录音需要您的录音权限") {
            block.invoke()
        }
    } else {
        block.invoke()
    }

}

/**
 * 是否有读取图片权限
 */
fun Activity.hasCameraPermission(): Boolean {
    return hasPermissions(
        permissions = listOf(
            Manifest.permission.CAMERA
        )
    )
}

/**
 * 是否有读取图片权限
 */
fun Activity.hasAudioPermission(): Boolean {
    return hasPermissions(
        permissions = listOf(
            Manifest.permission.RECORD_AUDIO
        )
    )
}

/**
 * 是否有读取图片权限
 */
fun Activity.hasReadImagePermission(): Boolean {
    return if (Build.VERSION.SDK_INT >= 34) {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_MEDIA_IMAGES
            )
        )

    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_MEDIA_IMAGES
            )
        )
    } else {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_EXTERNAL_STORAGE
            )
        )
    }
}

fun Activity.hasReadVideoPermission(): Boolean {
    return if (Build.VERSION.SDK_INT >= 34) {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_MEDIA_VIDEO
            )
        )

    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_MEDIA_VIDEO
            )
        )
    } else {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_EXTERNAL_STORAGE
            )
        )
    }
}

fun Activity.hasReadAudioPermission(): Boolean {
    return if (Build.VERSION.SDK_INT >= 34) {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_MEDIA_AUDIO
            )
        )

    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_MEDIA_AUDIO
            )
        )
    } else {
        hasPermissions(
            permissions = listOf(
                Manifest.permission.READ_EXTERNAL_STORAGE
            )
        )
    }
}

/**
 * 是否有存储权限
 */
fun getStoreP(): List<String> {
    return if (Build.VERSION.SDK_INT >= 34) {
        listOf(
//                Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED,
            Manifest.permission.READ_MEDIA_IMAGES,
            Manifest.permission.READ_MEDIA_VIDEO

        )

    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
        listOf(
            Manifest.permission.READ_MEDIA_IMAGES,
            Manifest.permission.READ_MEDIA_VIDEO

        )
    } else {

        listOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
    }
}

fun Context.hasPermissions(@Size(min = 1) permissions: List<String>): Boolean {
    if (Build.VERSION.SDK_INT < 23) {
        return true
    }
    for (perm in permissions) {
        if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
            return false
        }
    }
    return true
}


